<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
    <title>损耗录入</title>
    <link rel="stylesheet" href="/style/weui.css"/>
    <link rel="stylesheet" href="/style/swiper.min.css"/>
    <link rel="stylesheet" href="/style/example.css"/>
    <link rel="stylesheet" href="/style/jquery-weui.css">
    <link rel="stylesheet" href="/style/sweetalert.css"/>
    <style>
        .add-box>i:before { border: 15px solid transparent; }
    </style>
</head>
<body ontouchstart>
<div class="page">
    <div class="page__bd" style="height: 100%;">
        <div class="weui-tab">
            <div class="weui-navbar">
                <a class="weui-navbar__item weui-bar__item--on" href="#productBox">产品</a>
                <a class="weui-navbar__item" href="#materielBox">物料</a>
            </div>
            <div class="weui-tab__bd">
                <div id="productBox" class="weui-tab__bd-item weui-tab__bd-item--active">
                    <div class="weui-panel__bd">
                        <?php if(isset($data['pro'])) { foreach ($data['pro'] as $pro) { ?>
                            <div class="weui-media-box weui-media-box_appmsg">
                                <div class="weui-media-box__bd">
                                    <p class="weui-media-box__title">
                                        <?=$pro['name'] ?><br/>
                                        <span><?=$pro['categoryName'] ?></span>
                                    </p>
                                    <div style="position: absolute; right: 10px; bottom: 12px">
                                        <div class="weui-cell__ft add-box">
                                            <i class="icon iconfont icon-add" data="+1"></i>
                                            <input type="number" name="amount" style="width: 40px;display: inline-block" class="weui-input"
                                                   value="<?=$pro['qty'] ?>" data-id="<?=$pro['resId'] ?>" data-type="<?=$pro['type'] ?>">
                                            <i class="icon iconfont icon-reduce" style="display: inline-block" data="-1"></i>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php }} ?>
                    </div>
                </div>

                <div id="materielBox" class="weui-tab__bd-item">
                    <div class="weui-panel__bd">
                        <?php if(isset($data['mat'])) { foreach ($data['mat'] as $mat) { ?>
                            <div class="weui-media-box weui-media-box_appmsg">
                                <div class="weui-media-box__bd">
                                    <p class="weui-media-box__title">
                                        <?=$mat['name'] ?>&nbsp;&nbsp;<?=$mat['categoryName'] ?><br/>
                                        <span><?=$mat['specDesc'] ?></span><br/>
                                        <span><?=$mat['packDesc'] ?></span><br/>
                                        <?php if( isset($mat['ext1']) &&  $mat['ext1'] == 1) { ?>
                                            <span class="text-red">盘点单位:<?=$mat['useDesc']?></span>
                                        <?php } ?>
                                    </p>
                                </div>
                                <div class="weui-media-box__hd" style="width: 140px; height: auto;">
                                    <div class="weui-cell__ft add-box" style="margin-right:auto;">
                                        <i class="icon iconfont icon-add" data="+1"></i>
                                        <input type="number" name="amount" style="width: 25px;display: inline-block" class="weui-input"
                                               value="<?=$mat['qty'] ?>" data-id="<?=$mat['resId'] ?>" data-type="<?=$mat['type'] ?>">
                                        <i class="icon iconfont icon-reduce" style="display: inline-block" data="-1"></i>
                                    </div>
                                </div>
                            </div>
                        <?php }} ?>
                    </div>
                </div>
            </div>

            <div class="weui-form-preview__ft weui-footer_fixed-bottom" style="background-color: #fff; bottom: 0">
                <a class="weui-form-preview__btn" href="/inventory?pageFlg=1">返回</a>
                <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:saveCount()">保存</a>
            </div>
        </div>
    </div>
</div>
<script src="/js/zepto.min.js"></script>
<script src="/js/sweetalert.min.js"></script>
<script src="/js/swiper.min.js"></script>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="https://res.wx.qq.com/open/libs/weuijs/1.0.0/weui.min.js"></script>
<script src="/js/swiper.min.js"></script>
<script src="/js/jquery-weui.js"></script>
<script>

    $(function () {
        $(document).on("click", ".add-box i", function () {
            var data = parseInt($(this).attr("data")),
                val = parseInt($(this).siblings("input").val()),
                cnt = val + data;
            if (cnt > 0) {
                $(this).siblings().show();
            } else {
                cnt = 0;
            }
            $(this).siblings("input").val(cnt);
        });
    });

    function saveCount() {
        var selectData = [];
        $.each($('input[name=amount]'), function () {
            var qty = $(this).val();
            if (qty > 0) {
                selectData.push({'id': $(this).attr('data-id'), 'type': $(this).attr('data-type'), 'qty': qty})
            }
        });

        // if (!selectData.length) {
        //     swal('请选择录入数据');
        //     return;
        // }
        $.post(
            '/inventory/save-loss', {
                'lossId': '<?=$lossId ?>',
                'templateId': '<?=$templateId ?>',
                'selectData': selectData,
            },
            function (o) {
                if (o.code == 200) {
                    swal('保存成功');
                    setTimeout(function () {
                        location.href = '/inventory?pageFlg=1';
                    }, 1500);
                } else {
                    swal('保存失败');
                }
            },
            'json'
        );
    }

</script>
</body>
</html>
